﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EZMemoLib;

namespace UnitTestProject
{
    [TestClass()]
    public class MyBookListTest
    {


        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [TestMethod()]
        public void SaveMyBookTest()
        {
            //new MyBook { CategoryID = 1, LoginID = "0000", OrderNumber = 1 };
            long lngCategoryID = 2;
            
            MyBookList target = new MyBookList ();
            var category = new MemoCategory { CategoryID = lngCategoryID };

            int status = 0;

            bool isOK = false;
            try
            {
                target.SaveMyBook(category, status);

                isOK = true ;
            }
            catch
            { }

            Assert.IsTrue(isOK );

        }

        [TestMethod()]
        public void SaveMyBook()
        {
            long lCategoryID = 2;

            EZMemoEntities context = new EZMemoEntities();
            var list = context.MyBook.Where(c => c.CategoryID == lCategoryID);

            MyBook mybook = null;
            mybook = list.FirstOrDefault ();

            if (mybook == null)
            {
                mybook = new MyBook
                {
                    MyBookID = Guid.NewGuid(),
                    CategoryID = 1,
                    LoginID = "000",
                    StartDate = DateTime.Now,
                    ItemDone = 0,
                    ItemDrill = 0,
                    PlanTotal = 0,
                    Status = 1,
                    PlanUnit = 0,
                    TodayFlow = "",
                    TodayStatus = 0,
                    OrderNumber = 1,
                    LastDate = DateTime.Now
                };

                context.AddToMyBook(mybook);
            }
            else
            {
                mybook.Status = 100;
            }
            int rc = context.SaveChanges();

            Assert.IsTrue(rc > 0);
        }

        [TestMethod()]
        public void SaveConfig()
        {
            EZMemoEntities context = new EZMemoEntities();

            var conf = context.MemoConfig.FirstOrDefault();

            if (conf == null)
            {
                conf = new MemoConfig { ConfigName = "", ConfigValue = "", ConfigValueType = "" };
                context.AddToMemoConfig(conf);
            }
            else
            {
                conf.ConfigValue = DateTime.Now.ToString();
            }
            int rc = context.SaveChanges();

            Assert.IsTrue(rc > 0);
        }

        [TestMethod()]
        public void SaveMemoToday()
        {
            EZMemoEntities context = new EZMemoEntities();

            var today = context.MemoToday.Where ( c=>c.TodayID == 1).FirstOrDefault ();

            if (today == null)
            {
                today = new MemoToday { ItemID = 1, LoginID = "000", CheckType = ""};
                context.AddToMemoToday (today);
            }
            else
            {
                today.TodayDate = DateTime.Now;
            }
            int rc = context.SaveChanges();

            Assert.IsTrue(rc > 0);
        }
    }
}
